//
//  OrientationManagerTestViewController.m
//  JsUtilitySdkDemo_Dev
//
//  Created by Gikki Ares on 2023/6/15.
//

#import "OrientationManagerTestViewController.h"
#import <JsUtilitySdk/JsOrientationManager.h>

@interface OrientationManagerTestViewController (){
	
	__weak IBOutlet UISwitch *mSwitch_lockO;
	__weak IBOutlet UILabel *mLb_deviceo;
	__weak IBOutlet UILabel *mLb_uio;
	UIInterfaceOrientation  mInterfaceOrientation;
}



@end

@implementation OrientationManagerTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	UIInterfaceOrientation  o = [JsOrientationManager getCurrentInterfaceOrientation];
	NSString * str = [JsOrientationManager stringForUIInterfaceOrientation:o];
	mLb_uio.text = str;
	
	UIDeviceOrientation o2 = [JsOrientationManager getCurrentDeviceOrientation];
	NSString * str2 = [JsOrientationManager stringForDeviceOrientation:o2];
	mLb_deviceo.text = str2;
	
	[JsOrientationManager addInterfaceOrientationDidChangeHandler:^(UIInterfaceOrientation o) {
		[self onInterfaceOrientationUpdate];
	}];
	[JsOrientationManager addDeviceOrientationDidChangeHandler:^(UIDeviceOrientation o) {
		[self onDeviceOrientationUpdate];
	}];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)onClickBtn1:(id)sender {
	
	
}

- (IBAction)onSwitchValueChanged:(UISwitch *)sender {
	if(sender.isOn) {
		[self setNeedsUpdateOfSupportedInterfaceOrientations];
		mInterfaceOrientation = [JsOrientationManager getCurrentInterfaceOrientation];
	}
	else {
		if (@available(iOS 16.0, *)) {
			[self setNeedsUpdateOfSupportedInterfaceOrientations];
		} else {
				// Fallback on earlier versions
		}
	}
}
- (void)onInterfaceOrientationUpdate {
	UIInterfaceOrientation  o = [JsOrientationManager getCurrentInterfaceOrientation];
	NSString * str = [JsOrientationManager stringForUIInterfaceOrientation:o];
	mLb_uio.text = str;
}
- (void)onDeviceOrientationUpdate {
	UIDeviceOrientation o2 = [JsOrientationManager getCurrentDeviceOrientation];
	NSString * str2 = [JsOrientationManager stringForDeviceOrientation:o2];
	mLb_deviceo.text = str2;
}

- (IBAction)onClickBtn0:(id)sender {
	[self onInterfaceOrientationUpdate];
	[self onDeviceOrientationUpdate];
}

- (BOOL)isLockOrientation {
	return mSwitch_lockO.isOn;
}

- (BOOL)shouldAutorotate{
	if([self isLockOrientation]) {
		return NO;
	}
	else {
		return YES;
	}
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
	if([self isLockOrientation]) {
		return [JsOrientationManager interfaceOrientationMaskForOrientation:mInterfaceOrientation];
	}
	else {
		return UIInterfaceOrientationMaskAll;
	}
	
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
	return UIInterfaceOrientationPortrait;
}

@end
